<?php
namespace app\admin\controller;
use controller\BasicAdmin;
use think\Db;
class Adminuserrole extends BasicAdmin{
    public $table = 'user_role';
    public function index(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        $list = parent::_list($db,1,'','');

        foreach($list['list'] as $k=>$v){
            $description = json_decode($v['description']);
            $list['list'][$k]['des'] ='';
            for($a=0; $a < count($description);$a++){
                if(!is_array($description[$a]) && is_string($description[$a])){
                    $list['list'][$k]['des'] .= $description[$a];
                }else{
                    $list['list'][$k]['desc'] ='';
                    $desc = '';
                    for ($b=0;$b<count($description[$a]);$b++){
                         $desc .= $description[$a][$b];
                    }
                    $list['list'][$k]['desc'] ='(' . $desc . ')';
                }
            }
        }
        return $this->fetch('', ['title' => "用户角色管理",'list'=>$list]);
    }
    public function edit(){
        $get = $this->request->get();
        $info = Db::name($this->table)->where(['id'=>$get['id']])->find();

        $info['desc'] = json_decode($info['description']);
        $desc = [];
        foreach ($info['desc'] as $k=>$v){
            if($k==0){
                $desc[$k]['id'] = $k;
                $desc[$k]['name'] = '开通条件';
                $desc[$k]['data'] = $info['desc'][$k];
            }else if($k==1){
                if(is_array($v)){
                    $desc[$k]['id'] = $k;
                    $desc[$k]['name'] = '享受利益';
                    $desc[$k]['data'] = implode('',$v);
                }else{
                    $desc[$k]['id'] = $k;
                    $desc[$k]['name'] = '享受利益';
                    $desc[$k]['data'] = $info['desc'][$k];
                }
            }else if($k==2){
                $desc[$k]['id'] = $k;
                $desc[$k]['name'] = '提现要求';
                $desc[$k]['data'] = $info['desc'][$k];
            }
        }
        $info['desc'] = $desc;
        return $this->fetch('', ['title' => "编辑用户角色",'info'=>$info]);
    }

    public function postedit(){
        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $data['name'] = $post['name'];
        $data['status'] = $post['status'];
        $data['money'] = $post['money'];
        $data['new_recharge'] = $post['new_recharge'];
        $data['new_putmoney'] = $post['new_putmoney'];
        $data['max_pay'] = $post['max_pay'];
        $data['description'] = json_encode([$post[0],$post[1],$post[2]]);
        $result = $db->where(['id'=>$post['id']])->update($data);
        if($result){
            $this->success('修改成功','');
        }else{
            $this->error('修改失败','');
        }

    }
}